package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.LanguageCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleDeviceLanguage;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityLanguageBinding;
import com.apex.sdk.dialog.LanguageDialog;
import com.apex.sdk.dialog.WaitingDialog;

import java.util.List;


public class LanguageActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    private LanguageDialog languageDialog;
    private String tempLanguage;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                EABleDeviceLanguage eaBleBatInfo = (EABleDeviceLanguage) msg.obj;
                List<EABleDeviceLanguage.LanguageType> typeList = eaBleBatInfo.getSupportList();
                if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.default_type) {
                    activityLanguageBinding.language.setText(getString(R.string.language_default));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.english) {
                    activityLanguageBinding.language.setText(getString(R.string.language_english));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.chinese_simplifid) {
                    activityLanguageBinding.language.setText(getString(R.string.language_chinese_simplifid));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.chinese_traditional) {
                    activityLanguageBinding.language.setText(getString(R.string.language_chinese_traditional));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.korean) {
                    activityLanguageBinding.language.setText(getString(R.string.language_korean));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.thai) {
                    activityLanguageBinding.language.setText(getString(R.string.language_thai));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.japanese) {
                    activityLanguageBinding.language.setText(getString(R.string.language_japanese));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.spanish) {
                    activityLanguageBinding.language.setText(getString(R.string.language_spanish));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.francais) {
                    activityLanguageBinding.language.setText(getString(R.string.language_francais));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.deutsch) {
                    activityLanguageBinding.language.setText(getString(R.string.language_deutsch));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.italiano) {
                    activityLanguageBinding.language.setText(getString(R.string.language_italiano));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.polski) {
                    activityLanguageBinding.language.setText(getString(R.string.language_polski));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.portuguese) {
                    activityLanguageBinding.language.setText(getString(R.string.language_portuguese));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.russian) {
                    activityLanguageBinding.language.setText(getString(R.string.language_russian));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.dutch) {
                    activityLanguageBinding.language.setText(getString(R.string.language_dutch));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.arabic) {
                    activityLanguageBinding.language.setText(getString(R.string.language_arabic));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.greek) {
                    activityLanguageBinding.language.setText(getString(R.string.language_greek));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.hebrew) {
                    activityLanguageBinding.language.setText(getString(R.string.language_hebrew));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.swedish) {
                    activityLanguageBinding.language.setText(getString(R.string.language_swedish));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.osmanli) {
                    activityLanguageBinding.language.setText(getString(R.string.language_osmanli));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.czech) {
                    activityLanguageBinding.language.setText(getString(R.string.language_czech));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.indonesia) {
                    activityLanguageBinding.language.setText(getString(R.string.language_indonesia));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.melayu) {
                    activityLanguageBinding.language.setText(getString(R.string.language_melayu));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.farsi) {
                    activityLanguageBinding.language.setText(getString(R.string.language_farsi));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.vietnamese) {
                    activityLanguageBinding.language.setText(getString(R.string.language_vietnamese));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.belarusian) {
                    activityLanguageBinding.language.setText(getString(R.string.language_belarusian));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.hungarian) {
                    activityLanguageBinding.language.setText(getString(R.string.language_hungarian));
                } else if (eaBleBatInfo.getE_type() == EABleDeviceLanguage.LanguageType.hindi) {
                    activityLanguageBinding.language.setText(getString(R.string.language_hindi));
                }
                String supportLanguage = "";
                if (typeList != null && !typeList.isEmpty()) {
                    for (int i = 0; i < typeList.size(); i++) {
                        supportLanguage += (typeList.get(i).toString() + " ");
                    }
                }
                activityLanguageBinding.supportedLanguage.setText(supportLanguage);
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(LanguageActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityLanguageBinding.language.setText(tempLanguage);
            } else if (msg.what == 43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(LanguageActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityLanguageBinding activityLanguageBinding;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLanguageBinding = ActivityLanguageBinding.inflate(getLayoutInflater());
        setContentView(activityLanguageBinding.getRoot());
        activityLanguageBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityLanguageBinding.tool);
        getSupportActionBar().setTitle(null);
        activityLanguageBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (EABleManager.getInstance().getWatchType() == 1) {
            EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
            if (state == EABleConnectState.STATE_CONNECTED) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(LanguageActivity.this);
                }
                waitingDialog.show();
                EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.language, new LanguageCallback() {
                    @Override
                    public void languageInfo(EABleDeviceLanguage eaBleDeviceLanguage) {
                        if (mHandler != null) {
                            Message message = mHandler.obtainMessage();
                            message.what = 0x40;
                            message.obj = eaBleDeviceLanguage;
                            mHandler.sendMessage(message);
                        }
                    }


                    @Override
                    public void mutualFail(int errorCode) {
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x41);
                        }
                    }
                });
            }
        } else {
            activityLanguageBinding.supportLayout.setVisibility(View.GONE);
        }
        activityLanguageBinding.language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (languageDialog == null) {
                        languageDialog = new LanguageDialog(LanguageActivity.this);
                        languageDialog.setSelectListener(new LanguageDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                EABleDeviceLanguage deviceLanguage = new EABleDeviceLanguage();
                                if (sex.equalsIgnoreCase(getString(R.string.language_default))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.default_type);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_english))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.english);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_chinese_simplifid))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.chinese_simplifid);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_chinese_traditional))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.chinese_traditional);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_korean))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.korean);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_thai))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.thai);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_japanese))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.japanese);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_spanish))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.spanish);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_francais))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.francais);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_deutsch))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.deutsch);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_italiano))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.italiano);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_polski))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.polski);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_portuguese))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.portuguese);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_russian))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.russian);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_dutch))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.dutch);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_arabic))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.arabic);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_greek))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.greek);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_hebrew))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.hebrew);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_swedish))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.swedish);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_osmanli))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.osmanli);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_czech))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.czech);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_indonesia))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.indonesia);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_melayu))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.melayu);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_farsi))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.farsi);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_vietnamese))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.vietnamese);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_belarusian))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.belarusian);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_hungarian))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.hungarian);
                                } else if (sex.equalsIgnoreCase(getString(R.string.language_hindi))) {
                                    deviceLanguage.setE_type(EABleDeviceLanguage.LanguageType.hindi);
                                }
                                tempLanguage = sex;
                                EABleManager.getInstance().setDevLanguage(deviceLanguage, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x42);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!languageDialog.isShowing()) {
                        languageDialog.show();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (languageDialog != null) {
            languageDialog.dismiss();
            languageDialog.destroyDialog();
            languageDialog = null;
        }
        super.onDestroy();
    }
}
